<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php include("mainlist.php");?>
<table cellspacing="0" border="0" width="995">
	<tr>
		<td width="200" valign="top">					
			<?php
				include("menuleftcategory.php");
			?>
		</td>	
		<td width="795">
			<?php
				$id=$_GET['id'];
			?>
			<form action="index.php?admin=productAdmin&do=formeditproduct&id=<?php echo $id;?>" method="post" enctype="multipart/form-data">
			<table cellspacing="0" border="0" width="790">
				<tr>
					
					<td width="790" class="title_manager_cate">Sửa sản phẩm</td>
				
				</tr>
			</table>
			<table class="reference" cellspacing="0" border="1" width="100%">
				<tr>
					<td align="center" colspan="2">
						<?php
							$id=$_GET['id'];
							$current_category = $_GET['type'];
							$cateID = $_POST['cateID'];
							$groupID = $_POST['groupCat'];
							$productname = $_POST['productname'];
							
							$target="../upload/products/";
							
							$image = $_FILES['productImage']['name'];
							$link = $target.$image;
							
							
							$productDetail = $_POST['productDetail'];
							$productPrice = $_POST['productPrice'];
							$promotionPrice = $_POST['promotionPrice'];
							$prostyle = $_POST['prostyle'];
							$dvt = $_POST['dvt'];
							$productWarranty = $_POST['productWarranty'];
							
							if(!empty($_POST['productHot'])){
								$productHot = $_POST['productHot'];	
							}else{
								$productHot = 0;	
							}
							
							if(!empty($_POST['promotion'])){
								$promotion = $_POST['promotion'];
							}else{
								$promotion=0;
							}	
							
							$state = $_POST['state'];
							//print_r($state);
							
							if(isset($_POST['submit'])){

								move_uploaded_file($_FILES['productImage']['tmp_name'],$link);
								if($image==''){
									//echo "aaaa";die("aaaa");
									$query="UPDATE productdetails SET cateID='$cateID',groupID='$groupID',productname='$productname',productDetail='$productDetail',state='$state' WHERE id=$id";
									mysql_query($query);
								}else{
									//echo "bbb"; die("bbbb");
									$query="UPDATE productdetails SET cateID='$cateID',groupID='$groupID',productname='$productname',imgpath='$link',productDetail='$productDetail',productView='$productView',download='$download',productQty='$productQty',position='$position',dateCreated='$dateCreated',state='$state' WHERE id=$id";
									mysql_query($query);
								}
								
								if($productname==''){
									echo "<font color=\"red\">Bạn phải nhập vào tên sản phẩm.</font>";
								}else{
									mysql_query($query);
									$cate_id_arr = array();
									//Save productId to categories_products table
									if ($cateID){
										//Remove unselected catigories in categories_products table
										 	$query_cate_pro="select * from categories_products where productId=$id";
											$cate_pro_result = mysql_query($query_cate_pro);
											
											while($the_cate_pro_result = mysql_fetch_array($cate_pro_result))
											{
												array_push($cate_id_arr,$the_cate_pro_result[categoryId]);
											}
											foreach ($cate_id_arr as $aId)
											{
												if(!in_array($aId,$cateID,true))
												{
													$query1="DELETE FROM categories_products WHERE categoryId=$aId AND productId=$id";
													if(mysql_query($query1))
													{
														echo "Chỉnh sửa thành công.";
													}
													else 
													{
														echo "Chỉnh sửa không thành công.";
													}
												}
											}
											
											
										 //End revoving
										 foreach ($cateID as $aCateId)
										 {
										 	if(!in_array($aCateId,$cate_id_arr,true))

											{
										
												$query2 = "INSERT INTO categories_products(categoryId,productId) VALUES ('$aCateId','$id')";
												if(mysql_query($query2))
												{
													echo "Chỉnh sửa thành công.";
												}
												else 
												{
													echo "Chỉnh sửa không thành công.";
												}
											
												
											}
											
										 	
										 }
									}
									$current_category = $_POST['type'];
									echo "<script>window.location='index.php?admin=productAdmin&do=listgroup&id=$current_category'</script>";
								}
							}	
						?>
					</td>
				</tr>
			<?php
				$query="SELECT * FROM productdetails WHERE id=$id";
				$result=mysql_query($query);
				while($row=mysql_fetch_array($result)){?>
					<tr>
						<td width="150">Tên tài liệu:</td>
						<td>
							<input type="text" name="productname" size="40" value="<?php echo $row['productname']?>" /><font color="#FF0000"> * </font>
						</td>
					</tr>
				   
					<tr>
						<td>Danh mục:</td>
						<td>
							
								<select multiple name="cateID[]" id="cateID" > 
								<?php		
								//Get data from categories_products table
								$query_cate_pro="select * from categories_products where productId=$id ORDER BY id DESC ";
								$cate_pro_result = mysql_query($query_cate_pro);
								$cate_pro_id_arr = array();
								while($the_cate_pro_result = mysql_fetch_array($cate_pro_result))
								{
									array_push($cate_pro_id_arr,$the_cate_pro_result[categoryId]);
								}
								//end getting data			
								$query2 = "select * from categories";
								$result2 = mysql_query($query2);
								while($row2 = mysql_fetch_array($result2))
								{
									$categoriesId = $row2['id'];
									$selected ="";
									if(in_array($categoriesId, $cate_pro_id_arr, true))
									{
										$selected = "selected=\""."selected\"";
									}
									

								?>
									<option value="<?php echo $row2['id']?>"  <?php echo $selected ?> ><?php echo $row2['cateName'];?></option>	
								<?php
								}
								?>
							</select>
						</td>
					</tr>
					
					
					<tr>
						<td>File tài liệu:</td>
						<td>
							<input type="file" name="productImage" />
						</td>
					</tr>
					<tr>
						<td>Mô tả tài liệu:</td>
						<td>
							
						   <textarea  name="productDetail" id="textarea_full" rows="20" style="width:50%;"><?php echo $row['productDetail'];?></textarea>
						</td>
					</tr>
					
					<tr>
						<td>Trạng thái:</td>
						<td>
							<select name="state">
								<?php
									if($row['state']=='on'){?>
										<option value="on" selected="selected">Hiện tin</option>
										<option value="off">Ẩn tin</option>
								<?php
								}else{
								?>
								<option value="on">Hiện tin</option>		
								<option value="off" selected="selected">Ẩn tin</option>
								<?php
								}
								?>
							</select>
						</td>
					</tr>
			<?php
			}
			?>   	      	 
				   <tr>
						<td></td>
						<td>
							<input type="submit" name="submit" value="Sửa tài liệu" />
							<input type="button" name="Cancel" value="Cancel" onclick="window.location = 'index.php?admin=productAdmin&do=listgroup&id=<?php echo $current_category?>' " /> 
						<td>
				   </tr>
				</table>
				<input type="hidden" name="type" value="<?php echo $current_category?>" />
			</form>
		</td>
	</tr>	
</table>
</body>
</html>